package com.example.iotmanagerment26.controller;

import com.example.iotmanagerment26.domain.Device;
import com.example.iotmanagerment26.domain.DeviceHold;
import com.example.iotmanagerment26.domain.JsonData;
import com.example.iotmanagerment26.domain.vo.DeviceHoldVO;
import com.example.iotmanagerment26.service.DeviceHoldService;
import com.example.iotmanagerment26.service.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Tag(name="设备绑定解绑管理",description = "设备绑定解绑管理的接口")
public class DeviceHoldController {

    @Resource
    DeviceHoldService deviceHoldService;

    @PostMapping("/v1/devicehold/UserDeviceBind")
    @Operation(summary = "用户绑定设备")
    public JsonData UserDeviceBind(@RequestBody DeviceHold dh){
        int result=deviceHoldService.userBindDevice(dh);
        if(result>=1)
            return new JsonData(1,dh,"用户设备绑定成功");
        else
            return new JsonData(0,null,"用户设备绑定失败");
    }

    @GetMapping("selectUnbindDeviceByUser/{username}")
    @Operation(summary = "通过用户名字查询该用户已经解绑的所有设备")
    public Object selectUnbindDeviceByUser(@PathVariable String username){
        List<Device> devices=deviceHoldService.selectUnbindDeviceByUser(username);
        System.out.println(devices);
        if(devices!=null)
            return new JsonData(1,devices,"所有未绑定设备查询成功");
        else
            return new JsonData(0,null,"没有未绑定设备");
    }

    @GetMapping("/selectAllDeviceHoldList")
    @Operation(summary = "查询所有设备绑定的用户信息")
    public JsonData selectAllDeviceHoldList(){
        List<DeviceHoldVO> list = deviceHoldService.selectAllDeviceHoldList();
        System.out.println(list);
        if(list!=null)
            return new JsonData(1,list,"查询设备持有记录成功");
        else
            return new JsonData(0,null,"没有设备持有记录");
    }

    @PostMapping("/unBind")
    @Operation(summary = "用户解绑设备")
    public JsonData unBind(@RequestBody DeviceHold devicehold){
        devicehold.setHolded(0);
        int result=deviceHoldService.unBind(devicehold);
        if(result>=1)
            return new JsonData(1,devicehold,"用户设备解绑成功");
        else
            return new JsonData(0,devicehold,"用户设备解绑失败");
    }

    @GetMapping("selectbindDeviceByUser/{username}")
    @Operation(summary = "通过用户名字查询该用户绑定的所有设备")
    public Object selectbindDeviceByUser(@PathVariable String username){
        List<Device> devices=deviceHoldService.selectbindDeviceByUser(username);
        System.out.println(devices);
        if(devices!=null)
            return new JsonData(1,devices,"所有绑定设备查询成功");
        else
            return new JsonData(0,null,"没有绑定设备");
    }

}
